package xin.marcher.module.comment.service.impl;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.comment.domain.dto.*;
import xin.marcher.module.comment.domain.request.*;
import xin.marcher.module.comment.repository.SkuCommentRepository;
import xin.marcher.module.comment.service.SkuCommentService;
import xin.marcher.module.common.core.PageResult;

import java.util.Collections;
import java.util.Date;

/**
 * 商品评价信息性Service
 */
@Service
public class SkuCommentServiceImpl implements SkuCommentService {

    @Autowired
    private SkuCommentRepository skuCommentRepository;

    /**
     * 商品评价
     */
    @Override
    @Transactional
    public CommentResultDTO saveComment(SkuCommentRequest request) {
        // 查询出对应的待评价信息
        SkuWaitCommentDTO skuWaitCommentDTO = skuCommentRepository.getById(request.getWaitCommentId());
        // 校验时间在60天以内
        if (DateUtils.addDays(new Date(), -60).after(skuWaitCommentDTO.getBaseTime())) {
            return new CommentResultDTO(Boolean.FALSE);
        }
        // 保存对应的评价信息
        skuCommentRepository.saveComment(request, skuWaitCommentDTO);
        // 把对应的待评价信息修改为已完成
        skuCommentRepository.finishWaitComment(Collections.singletonList(skuWaitCommentDTO.getId()));
        return new CommentResultDTO(Boolean.TRUE);
    }

    /**
     * 商品追评
     */
    @Override
    public AdditionalSkuCommentResultDTO saveAddtionalComment(SkuAddtionalCommentRequst request) {
        return skuCommentRepository.saveAddtionalComment(request);
    }

    /**
     * 查询用户的商品评价列表
     */
    @Override
    public PageResult<SkuCommentDTO> listUserCommentPage(QueryUserCommentPageRequest request) {
        return skuCommentRepository.listUserCommentPage(request);
    }

    /**
     * 查询商品的商品评价列表
     */
    @Override
    public PageResult<SkuCommentDTO> listSkuCommentPage(QuerySkuCommentPageRequest request) {
        return skuCommentRepository.listSkuCommentPage(request);
    }

    /**
     * 审核初评和追评
     */
    @Override
    public AuditResultDTO auditComment(AuditCommentRequest request) {
        return skuCommentRepository.auditComment(request);
    }
}
